<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">企业信息管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>企业管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="company-search-key">
                <option value="">-请选择-</option>
                <option value="id">ID</option>
                <option value="companyName">公司名称</option>
                <option value="address">公司位置</option>
                <option value="financing">是否融资</option>
                <option value="evaluation">评价</option>
            </select>&emsp;
            <input id="company-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="company-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="company-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            <button id="company-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
        </div>

        <table class="layui-table" id="company-table" lay-filter="company-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="company-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var admin = layui.admin;

        //if( admin.hasPerm('user:add') ) {
        //   $('#btnAdd').hide();
        //}

        // 添加按钮绑定权限
        if(!admin.hasPerm('company:company/add')) {
            $('#company-btn-add').hide();
        }

        // 导出按钮绑定权限
        if(!admin.hasPerm('company:company/down')) {
            $('#company-btn-export').hide();
        }

        // 添加按钮点击事件
        $('#company-btn-add').click(function () {
            showEditModel();
        });

        // 渲染表格
        var company = table.render({
            elem: '#company-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-company/company/selectCompany',
            method: 'POST',
            where: {
                access_token: config.getToken().access_token,
                userId:config.getUser().id
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: '编号'},
                {field: 'companyName', sort: true, title: '公司名称'},
                {field: 'address', sort: true, title: '公司位置'},
                {field: 'financing', sort: true, title: '是否融资'},
                {field: 'companyDevelop', sort: true, title: '公司发展'},
                {field: 'companyProfile', sort: true, title: '公司简介'},
                {field: 'companyAdvantage', sort: true, title: '公司优势'},
                {field: 'companyTechniciansNum', sort: true, title: '公司人数'},
                {field: 'companyEstablish', sort: true, title: '公司成立于（年）'},
                {field: 'evaluation', sort: true, title: '评价'},
                {align: 'center', toolbar: '#company-table-bar', title: '操作',width: 150}
            ]]
        });



        // 工具条点击事件
        table.on('tool(company-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除这条数据吗？', function () {
                    layer.load(2);
                    admin.req('api-company/company/deleteCompany/'+obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('company-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'POST');
                });
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            admin.putTempData('t_company', data);
            var title = data ? '修改公司信息' : '添加公司信息';
            admin.popupCenter({
                title: title,
                path: 'pages/system/company_form.html',
                finish: function () {
                    table.reload('company-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#company-btn-search').click(function () {
            var key = $('#company-search-key').val();
            var value = $('#company-search-value').val();
            table.reload('company-table', {where: {userId:config.getUser().id,searchKey: key, searchValue: value}});
        });

        // 导出
        $('#company-btn-export').click(function () {
            var key = $('#company-search-key').val();
            var value = $('#company-search-value').val();
            admin.req('api-company/company/selectCompany', {userId:config.getUser().id,searchKey: key, searchValue: value}, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    table.exportFile(company.config.id, data.data);
                    layer.msg(data.msg, {icon: 1, time: 500});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
            }, 'POST');

        });
    });
</script>